package com.ljy.my_study.leetcode.数据流的中位数;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author lijunying
 * @date 2021/8/27 9:34
 */
public class MedianFinder {

    private PriorityQueue<Integer> maxQueue;
    private PriorityQueue<Integer> minQueue;

    public MedianFinder() {
        maxQueue=new PriorityQueue<>((a,b)->a.compareTo(b));
        minQueue=new PriorityQueue<>((a,b)->b.compareTo(a));
    }

    public void addNum(int num) {
        maxQueue.offer(num);
        if(minQueue.size()<maxQueue.size()){
            minQueue.offer(maxQueue.poll());
        }
    }

    public double findMedian() {
        if(minQueue.size()==maxQueue.size()){
            return (minQueue.peek()+maxQueue.peek())/2.0;
        }
        return minQueue.peek();
    }
}
